package com.example.suuu.tools;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailUtils {
    @Resource
    JavaMailSender javaMailSender;


    /**
     * 普通邮件
     *
     * @param from    发送人邮箱
     * @param to      接收人邮箱
     * @param cc      抄送人邮箱
     * @param subject 邮箱主题
     * @param content 邮箱内容
     */
    public void sendSimpleMail(String from, String to, String cc, String subject, String content) {
        MimeMessage message = mimeMessage();
        mimeMessageHelper(from, to, cc, subject, content,message);
        javaMailSender.send(message);
    }

    /**
     * 附件邮件
     *
     * @param from    发送人邮箱
     * @param to      接收人邮箱
     * @param cc      抄送人邮箱
     * @param subject 邮箱主题
     * @param content 邮箱内容
     * @param file    邮箱附件
     */
    public void sendAttachFileMail(String from, String to, String cc, String subject, String content, File file) {
        try {
            MimeMessage message = mimeMessage();
            MimeMessageHelper helper = mimeMessageHelper(from, to, cc, subject, content,message);
            helper.addAttachment(file.getName(), file);
            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片邮件
     *
     * @param from    发送人邮箱
     * @param to      接收人邮箱
     * @param cc      抄送人邮箱
     * @param subject 邮箱主题
     * @param content 邮箱内容
     * @param srcPath 图片路径
     * @param resIds  图片 ID
     */
    public void sendMailImg(String from, String to, String cc, String subject, String content, String[] srcPath, String[] resIds) {
        if (srcPath.length != resIds.length) {
            System.out.println("发送失败");
            return;
        }
        try {
            MimeMessage message = mimeMessage();
            MimeMessageHelper helper = mimeMessageHelper(from, to, cc, subject, content,message);
            for (int i = 0; i < srcPath.length; i++) {
                FileSystemResource res = new FileSystemResource(new File(srcPath[i]));
                helper.addInline(resIds[i], res);
            }
            javaMailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private MimeMessage mimeMessage() {
        return javaMailSender.createMimeMessage();
    }

    private MimeMessageHelper mimeMessageHelper(String from, String to, String cc, String subject, String content,MimeMessage message) {
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setBcc(cc);
            helper.setSubject(subject);
            helper.setText(content, true);
            return helper;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
